﻿Public Class frmBusquedaMuestra

    Private gestorMuestra As New BLmuestras
    Private codProtocolo As Integer
    Private codMedico As Integer
    Private codPaciente As Integer
    Private codDiagnostico As Integer



    Public Sub cargarDatos()

        Try
            Me.cmbTipMuestra.DataSource = gestorMuestra.ListarTipoMuestras
            Me.cmbTipMuestra.DisplayMember = "nombre"
            Me.cmbTipMuestra.ValueMember = "id_tipo_muestra"
            Me.cmbTipMuestra.SelectedIndex = 0

            Me.cmbOrgano.DataSource = gestorMuestra.ListarOrganos
            Me.cmbOrgano.DisplayMember = "nomOrg"
            Me.cmbOrgano.ValueMember = "id_organo"
            Me.cmbOrgano.SelectedIndex = 0

        Catch ex As Exception

            MsgBox(ex.ToString)

        End Try
    End Sub

    Public Sub limpiarCampos()
        Try
            Me.txtCodigo.Text = ""
            Me.codPaciente = 0
            Me.codProtocolo = 0
            Me.codMedico = 0
            Me.codDiagnostico = 0
            Me.txtEdadIni.Text = ""
            Me.txtEdadFin.Text = ""
            Me.cmbSexo.SelectedIndex = 0
            Me.cmbOrgano.SelectedText = 0
            Me.cmbTipMuestra.SelectedValue = 0
            Me.chkFechas.Checked = False

        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnDiagnostico_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDiagnostico.Click
        frmDiagnostico.ShowDialog()
        codDiagnostico = frmDiagnostico._codDiagnostico
        txtDiagnostico.Text = frmDiagnostico._nomDiagnostico

    End Sub

    Private Sub btnPaciente_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPaciente.Click
        frmPaciente.ShowDialog()
        codPaciente = frmPaciente._codPaciente
        txtPaciente.Text = frmPaciente._nomPaciente

    End Sub

    Private Sub btnMedico_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        frmMedicos.ShowDialog()
        codMedico = frmMedicos._codMedico
        txtMedico.Text = frmMedicos._nomMedico
    End Sub

    Private Sub btnBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscar.Click


        dgvResMuestras.DataSource = gestorMuestra.BuscarMuestras(Me.txtCodigo.Text, Me.codPaciente, Me.codMedico, Me.codDiagnostico, _
                                                                 Me.codProtocolo, Me.txtEdadIni.Text, Me.txtEdadFin.Text, Me.cmbSexo.SelectedIndex, _
                                                                 Me.cmbTipMuestra.SelectedValue, Me.cmbOrgano.SelectedValue, Me.DtpFechInicio.Value, Me.dtpFechFin.Value, Me.chkFechas.Checked)
        limpiarCampos()
        dgvResMuestras.Columns("codigo").HeaderText = "Código"
        dgvResMuestras.Columns("tipoMuestra").HeaderText = "Tipo Muestra"
        dgvResMuestras.Columns("paciente").HeaderText = "Paciente"
        dgvResMuestras.Columns("edad").HeaderText = "Edad"
        dgvResMuestras.Columns("sexo").HeaderText = "Sexo"
        dgvResMuestras.Columns("diagnostico").HeaderText = "Diagnóstico"
        dgvResMuestras.Columns("organo").HeaderText = "Órgano"
        dgvResMuestras.Columns("medico").HeaderText = "Médico"
        dgvResMuestras.Columns("departamento").HeaderText = "Departamento"
        dgvResMuestras.Columns("fechaIng").HeaderText = "Fecha de Ingreso"
        dgvResMuestras.Columns("observaciones").HeaderText = "Observaciones"
    

    End Sub

    Public Property _codDiagnostico()
        Get
            Return Me.codDiagnostico
        End Get
        Set(ByVal value)
            Me.codDiagnostico = value
        End Set
    End Property

    Public Property _diagnostico()
        Get
            Return Me.txtDiagnostico.Text
        End Get
        Set(ByVal value)

            Me.txtDiagnostico.Text = value

        End Set
    End Property

    
    Private Sub btncancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancelar.Click
        Me.Close()
    End Sub

    Private Sub btnLimpiar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLimpiar.Click
        Try
            Me.txtCodigo.Clear()
            Me.txtPaciente.Clear()
            Me.txtProtocolo.Clear()
            Me.txtDiagnostico.Clear()
            Me.txtEdadIni.Clear()
            Me.txtEdadFin.Clear()
            Me.cmbSexo.SelectedIndex = 0
            Me.cmbTipMuestra.SelectedIndex = 0
            Me.cmbOrgano.SelectedIndex = 0
            Me.chkFechas.Checked = False

        Catch ex As Exception

        End Try

    End Sub

    Private Sub frmBusquedaMuestra_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        cargarDatos()
        Me.cmbSexo.SelectedIndex = 0
        



    End Sub


    Private Sub btnAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregar.Click
        frmMantenimientoMuestra.ShowDialog()
    End Sub

    Private Sub btnModificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModificar.Click
        Dim codMuestraMod As Integer
        Dim frmMuestra As New frmMantenimientoMuestra

        If IsNothing(dgvResMuestras.CurrentRow) Then
            MsgBox("Debe seleccionar una muestra")
        Else
            codMuestraMod = dgvResMuestras.CurrentRow.Cells("codigo").Value
            frmMuestra._esModificar = True
            frmMuestra._codMuestra = codMuestraMod
            frmMuestra.ShowDialog()
        End If
    End Sub
End Class